Read in data

lizards <- read_csv(here("data_tidy", "lizards.csv"))
## Rows: 1628 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): date, scientific_name, common_name, zone, site, plot, spp, sex, rc...
## dbl  (6): pit, toe_num, sv_length, total_length, weight, pc
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ggplot(lizards, aes(x = total_length, y = weight)) + 
  geom_point()

lizard_hist <- ggplot(lizards, aes(x = total_length)) + 
  geom_histogram(color = "orange", 
                 fill = "blue", 
                 linetype = "dotted")

lizard_hist
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point(shape = 22, color = "cyan4", 
             fill = "yellow", size = 2, 
             alpha = 0.5)

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point(aes(color = common_name, size = total_length)) +
  theme_minimal()

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point() + 
  facet_wrap(~common_name, scales = "free")

count_liz <- lizards |> 
  group_by(common_name) |>
  summarize(
    counts = n()
  )

count_lizards <- lizards |> 
  count(common_name, site, tail)
ggplot(count_liz, 
       aes(y = fct_reorder(common_name, counts), 
           x = counts)) + 
  geom_col()

Four cats? Factor reorder

changes the order of a list of characters/strings

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point(aes(color = common_name)) + 
  facet_wrap(~common_name, scales = "free") + 
  theme_minimal() + 
  labs(x = "Total Length (mm)", 
       y = "Weight (g)", 
       title = "Lizard Sizes",
       subtitle = "All lizards follow the length-width model", 
       caption = "Cite data source")

Fun with scales

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point(aes(color = weight)) + 
  scale_color_gradient(low = "purple", high = "orange")

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point(aes(color = weight)) + 
  scale_color_gradientn(colors = c("magenta", 
                                   "cyan4", "green", 
                                   "yellow", "dodgerblue"))

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point(aes(color = weight)) + 
  scale_color_steps(low = "red", high = "black")

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point(aes(color = total_length)) + 
  scale_color_steps2(low = "purple", mid = "grey", high = "orange", 
                     midpoint = 150, 
                     breaks = c(50, 100, 150, 200, 250, 300, 350, 400))

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point(aes(color = total_length)) + 
  scale_color_stepsn(colors = c("orange", "red", "purple"),  
                     breaks = seq(from = 0, to = 400, by = 75))

factor_lizards <- lizards |> 
  mutate(common_name = fct_reorder(common_name, 
                                   total_length, 
                                   .fun = median))

class(factor_lizards$common_name)
## [1] "factor"
ggplot(data = factor_lizards, aes(x = total_length, 
                                  y = common_name)) + 
  geom_boxplot(aes(fill = common_name), show.legend = FALSE) + 
  scale_fill_paletteer_d(palette = "tvthemes::AirNomads") + 
  theme_minimal()

pal_filt <- filter(palettes_d_names, length == 7)
ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point(aes(color = weight)) + 
  theme(panel.grid.major.x = element_line(color = "red"), 
        panel.grid.minor.y = element_blank(), 
        axis.title.x = element_text(color = "orange"), 
        axis.text.y = element_text(color = "blue"), 
        text = element_text(size = 14), 
        panel.background = element_rect(color = "green", 
                                        fill = "yellow")) + 
  annotate("text", x = 300, y = 50, 
           label = "Look here", color = "blue") + 
  geom_vline(xintercept = 250, linetype = "dashed", color = "purple", 
             size = 4)

Repulsive labels with ggrepel

wws_lizards <- lizards |> 
  filter(common_name == "western whiptail", site == "sand")

ggplot(data = wws_lizards, aes(x = total_length, y = weight)) + 
  geom_point() + 
  geom_text_repel(aes(label = toe_num))

gapminder

gapminder_euro <- gapminder |> 
  filter(year == 2002, continent == "Europe") |> 
  ggplot(aes(x = gdpPercap, y = lifeExp)) + 
  geom_point() + 
  geom_text_repel(aes(label = country), size = 3)

gapminder_euro

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point() + 
  gghighlight(toe_num == 250, label_key = toe_num)

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point() + 
  gghighlight(weight > 30, label_key = toe_num)

###Using sf and maps

spatial_veg <- read_sf(here("data_raw", 
                            "spatial_vegetation", "doc.kml"))

ggplot() + 
  geom_sf(data = spatial_veg, 
          aes(fill = Name), color = NA) + 
  theme_minimal() + 
  scale_fill_paletteer_d(palette = "ggthemes::manyeys")

Make a heat map

lizard_counts <- lizards |>
  mutate(date = mdy(date)) |>
  count(year = year(date), common_name) |> 
  drop_na()
ggplot(data = lizard_counts, aes(x = year, y = common_name)) + 
  geom_tile(aes(fill = n), show.legend = FALSE) + 
  geom_text(aes(label = n), color = "white", size = 3) + 
  scale_fill_gradientn(colors = c("navy", "red", "orange")) +
  theme_minimal()

Make a Beeswarm plot

whiptails <- lizards |> 
  filter(common_name == "western whiptail") |> 
  drop_na(total_length, weight)
whiptail_beeswarm <- ggplot(data = whiptails, 
                            aes(x = sex, y = weight)) + 
  geom_beeswarm() + 
  geom_boxplot(fill = NA)

Marginal Plot

whiptail_plot <- ggplot(data = whiptails, 
                        aes(x = total_length, y = weight)) + 
  geom_point(aes(color = sex)) + 
  theme(legend.position = "bottom")

ggMarginal(whiptail_plot, type = "boxplot", groupColour = TRUE)

Patchwork!

# Patchwork allows for multiple plots to be put together in the same image. + puts them side by side (columns)
(whiptail_beeswarm + whiptail_plot)

# dividing (/) puts them in rows/one below the other
((whiptail_beeswarm + whiptail_plot)/ whiptail_beeswarm) & theme_minimal()